package com.wkg.lotterysystem.service.activitystatus.operater;

import com.wkg.lotterysystem.dao.dataobject.ActivityUserDo;
import com.wkg.lotterysystem.dao.mapper.ActivityUserMapper;
import com.wkg.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.wkg.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator {

    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Integer activityId = convertActivityStatusDTO.getActivityId();
        List<Integer> userIds = convertActivityStatusDTO.getUserId();
        ActivityUserStatusEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();
        if (null == activityId
                || null == userIds
                || null == targetUserStatus) {
            return false;
        }
        List<ActivityUserDo> activityUserDoList = activityUserMapper.batchSelectByAUIds(activityId, userIds);
        if (CollectionUtils.isEmpty(activityUserDoList)) {
            return false;
        }
        for (ActivityUserDo activityUserDo : activityUserDoList) {
            if (activityUserDo.getStatus().equalsIgnoreCase(targetUserStatus.name())) {
                return false;
            }
        }

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Integer activityId = convertActivityStatusDTO.getActivityId();
        List<Integer> userIds = convertActivityStatusDTO.getUserId();
        ActivityUserStatusEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();

        try {
            activityUserMapper.batchUpdateStatus(activityId, userIds, targetUserStatus.name());

            return true;
        } catch (Exception e) {
            return false;
        }

    }
}
